importScripts('include/config.js');
importScripts('API/js/fonctions.js');
importScripts('js/fonctions.js');

function messageHandler(event) {
    try {
        // On récupère le message envoyé par la page principale
        var messageSent = event.data;

        // On teste la commande envoyée
        switch (messageSent.cmd) {
            case 'init':
                // On peut initialiser certaines parties de nos objets qui serviront
                // dans ce worker (attention au scope cependant !)
                break;
            case 'calcule':
                var entree = new Date;
                var entree = entree.getTime();
                
                var dateBegin = messageSent.parameter.dateBegin;
                var dateEnd = messageSent.parameter.dateEnd;
                var dateStatutBegin = messageSent.parameter.dateStatutBegin;
                var dateStatutEnd = messageSent.parameter.dateStatutEnd;

                var str_sql = "SELECT domaine FROM tab_inventaire_domaines WHERE 1=1 AND macro_domaine = 'Autres'";
                var tab_dom = getSQL(str_sql);

                tabDataUp = new Array();

                for (var indiceDom in tab_dom) {
                    var percent = Math.round((indiceDom / tab_dom.length) * 100);
                    this.postMessage(new WorkerMessage('avancement', percent));

                    var str_domaine = tab_dom[indiceDom][0];

                    var tab = new Array();
                    tab = getTabEvolDomBacklog(dateBegin, dateEnd, dateStatutBegin, dateStatutEnd, str_domaine);
                    tab = tab.splice(1, tab.length - 1);

                    tabDataMiddle = new Array();
                    tabDataDown = new Array();

                    for (var indice in tab) {
                        jour = tab[indice][0].substring(8, 10);
                        mois = String((tab[indice][0].substring(5, 7)) - 1);
                        annee = tab[indice][0].substring(0, 4);
                        tabDataDown = new Array(Date.UTC(annee, mois, jour), parseInt(tab[indice][1]));
                        tabDataMiddle[tabDataMiddle.length] = tabDataDown;
                    }

                    tabDataUp[tabDataUp.length] = {
                        name: str_domaine,
                        data: tabDataMiddle
                    };
                }

                var fin = new Date;
                var fin = fin.getTime();
                var secondes = Math.round((fin-entree)/1000);
                var hhmmss = convertSecondsToTime(secondes);
                this.postMessage(new WorkerMessage('stats',hhmmss));

                this.postMessage(new WorkerMessage('resultats',tabDataUp));
                break;
            }
    }catch (er) {
        this.postMessage("Erreur : " + er.message);
        log(0, "ERROR(WorkerEvolStockAutres):" + er.message);
    }
}

// On définit la fonction à appeler lorsque la page principale nous sollicite
this.addEventListener('message', messageHandler, false);

function getTabEvolDomBacklog(p_dateBegin, p_dateEnd, p_dateStatutBegin, p_dateStatutEnd, p_domaine) {
    try {
        var strResponse = "";
        var returns = new Array();
        var tempTab = new Array();
        var tempSubTab = new Array();

        xhr_object = new AJ();

        var url = "phpsql/mysql_getEvolDomBacklog.php?milis=" + getMilise() + "&dateBegin=" + p_dateBegin + "&dateEnd=" + p_dateEnd + "&dateStatutBegin=" + p_dateStatutBegin + "&dateStatutEnd=" + p_dateStatutEnd + "&domaine=" + p_domaine;

        xhr_object.open("GET", url, false);
        xhr_object.send(null);
        if (xhr_object.readyState == 4) {
            strResponse = xhr_object.responseText;
        } else {
            strResponse = "ERROR";
        }

        tempTab = strResponse.split("\n");

        for (var indice in tempTab) {
            tempSubTab = tempTab[indice].split("&");
            if (tempSubTab.length > 1) {
                tab = new Array(tempSubTab[0], parseFloat(tempSubTab[1]));
                returns[returns.length] = tab;
            }
        }

        return returns;
    }
    catch (er) {
        log(0, "ERROR(getTabEvolDomBacklog):" + er.message);
    } finally {
        delete xhr_object;
    }
}